package fr.inserm.umr915.libjson;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.w3c.dom.Document;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

public abstract class FloatNode extends Node
	{
	static final String TAG="float";
	private double value;
	FloatNode(double value)
		{
		this.value=value;
		}
	
	@Override
	public Node clone(NodeFactory owner) {
		return owner.createFloatNode(getValue());
		}
	
	public double getValue()
		{
		return this.value;
		}
	@Override
	public NodeType getNodeType()
		{
		return NodeType.FLOAT;
		}
	
	@Override
	public int hashCode() {
		return new Double(getValue()).hashCode();
		}
		
	@Override
	public void write(DataOutputStream out) throws IOException
		{
		out.writeByte((byte)getNodeType().ordinal());
		out.writeDouble(getValue());
		}
	
	@Override
	public void print(PrintWriter out)
		{
		out.print(getValue());
		}
	
	@Override
	public org.w3c.dom.Element asDom(Document owner)
		{
		org.w3c.dom.Element root= owner.createElement(TAG);
		root.appendChild(owner.createTextNode(String.valueOf(getValue())));
		return root;
		}
	
	@Override
	public void write(XMLStreamWriter out) throws XMLStreamException
		{
		out.writeStartElement(TAG);
		out.writeCharacters(String.valueOf(getValue()));
		out.writeEndElement();
		}
	
	@Override
	public String toString()
		{
		return String.valueOf(getValue());
		}
	
	@Override
	protected void saxHandler(ContentHandler handler) throws SAXException,
			IOException
		{
		handler.startElement("","", TAG, new AttributesImpl());
		char ch[]=String.valueOf(getValue()).toCharArray();
		handler.characters(ch, 0, ch.length);
		handler.endElement("", "",TAG);
		}
	
}